package com.lanyan.system.controller.system;

import java.math.BigDecimal;
import java.util.List;
import java.util.Date;
import javax.servlet.http.HttpServletResponse;

import com.lanyan.common.utils.StringUtils;
import com.lanyan.system.domain.Account;
import com.lanyan.system.domain.BalanceHistory;
import com.lanyan.system.domain.vo.AccountVo;
import com.lanyan.system.service.IAccountService;
import com.lanyan.system.service.IBalanceHistoryService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lanyan.common.annotation.Log;
import com.lanyan.common.core.controller.BaseController;
import com.lanyan.common.core.domain.AjaxResult;
import com.lanyan.common.enums.BusinessType;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import com.lanyan.system.domain.Withdraw;
import com.lanyan.system.service.IWithdrawService;
import com.lanyan.common.utils.poi.ExcelUtil;
import com.lanyan.common.core.page.TableDataInfo;

/**
 * 提现Controller
 *
 * @author lanyan
 * @date 2024-11-26
 */
@RestController
@RequestMapping("/system/withdraw")
public class WithdrawController extends BaseController
{
    @Autowired
    private IWithdrawService withdrawService;
    @Autowired
    private IAccountService accountService;
    @Autowired
    private IBalanceHistoryService balanceHistoryService;

    /**
     * 查询提现列表
     */
    @PreAuthorize("@ss.hasPermi('system:withdraw:list')")
    @GetMapping("/list")
    @ApiImplicitParams({
          @ApiImplicitParam(name = "createBy", value = "创建者", dataType = "String", dataTypeClass = String.class),
          @ApiImplicitParam(name = "createTime", value = "创建时间", dataType = "Date", dataTypeClass = Date.class),
          @ApiImplicitParam(name = "updateBy", value = "更新者", dataType = "String", dataTypeClass = String.class),
          @ApiImplicitParam(name = "updateTime", value = "更新时间", dataType = "Date", dataTypeClass = Date.class),
          @ApiImplicitParam(name = "remark", value = "备注", dataType = "String", dataTypeClass = String.class),
          @ApiImplicitParam(name = "accountId", value = "所属用户", dataType = "Long", dataTypeClass = Long.class),
          @ApiImplicitParam(name = "money", value = "提现金额", dataType = "BigDecimal", dataTypeClass = BigDecimal.class),
          @ApiImplicitParam(name = "cause", value = "决绝原因", dataType = "String", dataTypeClass = String.class),
          @ApiImplicitParam(name = "pageNum", value = "第几页", dataType = "int", dataTypeClass = int.class),
          @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", dataTypeClass = int.class),

    })
    public TableDataInfo list(Withdraw withdraw)
    {
        startPage();
        List<Withdraw> list = withdrawService.selectWithdrawList(withdraw);
        for (Withdraw item : list) {
            item.setAccount(accountService.getById(item.getAccountId()));
        }
        return getDataTable(list);
    }

    /**
     * 导出提现列表
     */
    @PreAuthorize("@ss.hasPermi('system:withdraw:export')")
    @Log(title = "提现", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Withdraw withdraw)
    {
        List<Withdraw> list = withdrawService.selectWithdrawList(withdraw);
        ExcelUtil<Withdraw> util = new ExcelUtil<Withdraw>(Withdraw.class);
        util.exportExcel(response, list, "提现数据");
    }

    /**
     * 获取提现详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:withdraw:query')")
    @GetMapping(value = "/{withdrawId}")
    public AjaxResult getInfo(@PathVariable("withdrawId") Long withdrawId)
    {
        return success(withdrawService.selectWithdrawByWithdrawId(withdrawId));
    }

    /**
     * 新增提现
     */
    @PreAuthorize("@ss.hasPermi('system:withdraw:add')")
    @Log(title = "提现", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiImplicitParams({
                    @ApiImplicitParam(name = "createBy", value = "创建者", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "createTime", value = "创建时间", dataType = "Date", dataTypeClass = Date.class),
                    @ApiImplicitParam(name = "updateBy", value = "更新者", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "updateTime", value = "更新时间", dataType = "Date", dataTypeClass = Date.class),
                    @ApiImplicitParam(name = "remark", value = "备注", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "accountId", value = "所属用户", dataType = "Long", dataTypeClass = Long.class),
                    @ApiImplicitParam(name = "money", value = "提现金额", dataType = "BigDecimal", dataTypeClass = BigDecimal.class),
                    @ApiImplicitParam(name = "cause", value = "决绝原因", dataType = "String", dataTypeClass = String.class),
    })
    public AjaxResult add(@RequestBody Withdraw withdraw)
    {
        return toAjax(withdrawService.insertWithdraw(withdraw));
    }

    /**
     * 修改提现
     */
    @PreAuthorize("@ss.hasPermi('system:withdraw:edit')")
    @Log(title = "提现", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiImplicitParams({
                    @ApiImplicitParam(name = "createBy", value = "创建者", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "createTime", value = "创建时间", dataType = "Date", dataTypeClass = Date.class),
                    @ApiImplicitParam(name = "updateBy", value = "更新者", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "updateTime", value = "更新时间", dataType = "Date", dataTypeClass = Date.class),
                    @ApiImplicitParam(name = "remark", value = "备注", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "accountId", value = "所属用户", dataType = "Long", dataTypeClass = Long.class),
                    @ApiImplicitParam(name = "money", value = "提现金额", dataType = "BigDecimal", dataTypeClass = BigDecimal.class),
                    @ApiImplicitParam(name = "cause", value = "决绝原因", dataType = "String", dataTypeClass = String.class),
    })
    public AjaxResult edit(@RequestBody Withdraw withdraw)
    {
        if (StringUtils.equals(withdraw.getSate(),"2")){
            Withdraw old = withdrawService.getById(withdraw.getWithdrawId());
            Account account = accountService.getById(old.getAccountId());
            accountService.lambdaUpdate()
                    .eq(AccountVo::getAccountId,account.getAccountId())
                    .set(AccountVo::getBalance,account.getBalance().add(old.getMoney()))
                    .update();
            BalanceHistory balanceHistory = new BalanceHistory();
            balanceHistory.setAccountId(account.getAccountId());
            balanceHistory.setMessage("提现驳回");
            balanceHistory.setTitle("提现驳回");
            balanceHistory.setMoney(withdraw.getMoney());
            balanceHistory.setType("0");
            balanceHistory.setPointTo(withdraw.getWithdrawId().toString());
            balanceHistoryService.save(balanceHistory);
        }
        return toAjax(withdrawService.updateWithdraw(withdraw));
    }

    /**
     * 删除提现
     */
    @PreAuthorize("@ss.hasPermi('system:withdraw:remove')")
    @Log(title = "提现", businessType = BusinessType.DELETE)
	@DeleteMapping("/{withdrawIds}")
    public AjaxResult remove(@PathVariable Long[] withdrawIds)
    {
        return toAjax(withdrawService.deleteWithdrawByWithdrawIds(withdrawIds));
    }
}
